#ifndef __COMMON_H__
#define __COMMON_H__

/*****************************************************************************/
/* Include Files */

#include "Config.h"
#include "Types.h"
#include "Status.h"

#ifdef USE_STANDARD_LIBRARIES
#include <string.h>
#endif /*USE_STANDARD_LIBRARIES*/

/*****************************************************************************/
/* Macro Definitions */

#define TMIN(X, Y)  ((X) < (Y) ? (X) : (Y))
#define TMAX(X, Y)  ((X) > (Y) ? (X) : (Y))
#define TDIFF(X, Y) ((X) > (Y) ? (X) - (Y) : (Y) - (X))
#define TABS(X)     ((X) >= 0 ? (X) : -(X))

/*****************************************************************************/
/* Function Prototypes */

#ifdef __cplusplus
extern "C" {
#endif /*__cplusplus*/

TUINT16 Read_TUINT16(const TUINT8* buffer,
                     TUINT32       position);

TUINT32 Read_TUINT32(const TUINT8* buffer,
                     TUINT32       position);

void Write_TUINT16(TUINT8* buffer,
                   TUINT32 position,
                   TUINT16 data);

void Write_TUINT32(TUINT8* buffer,
                   TUINT32 position,
                   TUINT32 data);

#ifdef USE_STANDARD_LIBRARIES
#define Memory_Fill(BUFFER, VALUE, SIZE) memset((BUFFER), (VALUE), (SIZE))

#define Memory_Copy(DEST, SRC, SIZE) memcpy((DEST), (SRC), (SIZE))

#define Memory_Compare(BUFFER1, BUFFER2, SIZE) (TINT32) memcmp((BUFFER1), (BUFFER2), (SIZE))

#define String_Length(STRING) (TUINT32) strlen(STRING)

#define String_Copy(DEST, SRC) strcpy((DEST), (SRC))

#define String_Append(DEST, SRC) strcat((DEST), (SRC))

#define String_Compare(STRING1, STRING2) (TINT32) strcmp((STRING1), (STRING2))
#else
void Memory_Fill(void*   memory,
                 TUINT8  value,
                 TUINT32 size);

void Memory_Copy(void*       dest,
                 const void* src,
                 TUINT32     size);

TINT32 Memory_Compare(const void* memory1,
                      const void* memory2,
                      TUINT32 size);

TUINT32 String_Length(const TUINT8* str);

void String_Copy(TUINT8*       dest,
                 const TUINT8* src);

void String_Append(TUINT8*       dest,
                   const TUINT8* src);

TINT32 String_Compare(const TUINT8* str1,
                      const TUINT8* str2);
#endif /*USE_STANDARD_LIBRARIES*/

TUINT32 Read_DecNumber(const TUINT8* str);

TUINT32 Read_HexNumber(const TUINT8* str);

#ifdef __cplusplus
}
#endif /*__cplusplus*/

/*****************************************************************************/
/* End of File */

#endif /*__COMMON_H__*/
